#!/bin/bash
# chrome_installer.sh

# ---------- 配置参数 ----------
CHROME_DEB="google-chrome-stable_current_amd64.deb"  # Chrome安装包名
CHROMEDRIVER_SRC="./chromedriver"                   # 当前目录源文件
INSTALL_DIR="/usr/local/bin"                        # 安装路径
FFMPEG_TAR="ffmpeg-release-amd64-static.tar.xz"     # 当前目录FFmpeg包

# ---------- 预处理设置 ----------
set -euo pipefail  # 启用严格错误检查模式
clear

# ---------- 函数定义 ----------
error_exit() {
    echo -e "\033[31m错误: $1\033[0m" >&2
    exit 1
}

check_deb() {
    [ -f "$1" ] || error_exit "未找到安装包: $1"
}

check_file() {
    [ -f "$1" ] || error_exit "未找到文件: $1"
}

# ---------- 1. 系统环境准备 ----------
echo -e "\n\033[34m[1/5] 正在配置系统环境...\033[0m"
sed -i 's/main$/main contrib non-free universe/' /etc/apt/sources.list
apt-get update -qq

# ---------- 2. 安装系统依赖 ----------
echo -e "\n\033[34m[2/5] 正在安装系统依赖库...\033[0m"
apt-get install -y --install-recommends \
    fonts-liberation libasound2 libasound2-data libatk-bridge2.0-0 \
    libatk1.0-0 libatk1.0-data libatspi2.0-0 libcairo2 libcairo-gobject2 \
    libcups2 libdrm2 libdrm-common libgbm1 libglib2.0-0 libglib2.0-data \
    libgtk-3-0 libgtk-3-common libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 \
    libpangoft2-1.0-0 libpixman-1-0 libthai0 libthai-data libwayland-client0 \
    libwayland-cursor0 libwayland-egl1 libwayland-server0 libx11-6 libx11-xcb1 \
    libxcb-render0 libxcb-shm0 libxcomposite1 libxcursor1 libxdamage1 libxext6 \
    libxfixes3 libxi6 libxinerama1 libxkbcommon0 libxrandr2 libxrender1 xdg-utils \
    libvulkan1 libu2f-udev shared-mime-info hicolor-icon-theme adwaita-icon-theme \
    xkb-data libfribidi0 libharfbuzz0b libepoxy0 libjson-glib-1.0-0 librest-0.7-0 \
    libavahi-client3 libavahi-common3 fontconfig > /dev/null

apt-get --fix-broken install -y

# ---------- 3. 安装Chrome浏览器 ----------
echo -e "\n\033[34m[3/5] 正在安装Chrome浏览器...\033[0m"
check_deb "$CHROME_DEB"
dpkg -i "$CHROME_DEB" || apt-get install -f -y

# ---------- 4. 部署Chromedriver（复制模式） ----------
echo -e "\n\033[34m[4/5] 正在部署Chromedriver...\033[0m"
if check_file "$CHROMEDRIVER_SRC"; then
    echo "检测到Chromedriver文件，设置权限并复制..."
    chmod +x "$CHROMEDRIVER_SRC"
    cp -vpf "$CHROMEDRIVER_SRC" "$INSTALL_DIR/chromedriver"
    
    # 版本检查
    CHROME_VERSION=$(google-chrome --version | awk '{print $3}')
    DRIVER_VERSION=$("$INSTALL_DIR/chromedriver" --version | awk '{print $2}')
    
    if [ "$CHROME_VERSION" != "$DRIVER_VERSION" ]; then
        echo -e "\033[33m警告: Chrome版本($CHROME_VERSION)与驱动版本($DRIVER_VERSION)不匹配\033[0m"
    else
        echo -e "\033[32m版本验证通过: v$CHROME_VERSION\033[0m"
    fi
fi

# ---------- 5. 安装本地FFmpeg ----------
echo -e "\n\033[34m[5/5] 正在安装FFmpeg...\033[0m"
check_file "$FFMPEG_TAR"

WORK_DIR=$(mktemp -d)
echo "创建临时工作目录: $WORK_DIR"

# 解压并安装
echo "解压FFmpeg安装包..."
tar xf "$FFMPEG_TAR" -C "$WORK_DIR" || error_exit "解压失败"

cd "$WORK_DIR/ffmpeg-"*"-static" || error_exit "进入解压目录失败"

echo "安装二进制文件到系统:"
install -v -m 755 ffmpeg ffprobe "$INSTALL_DIR"
ln -sf "$INSTALL_DIR/ffmpeg" /usr/bin/ffmpeg

# 清理临时文件
cd /tmp && rm -rf "$WORK_DIR"

# ---------- 安装验证 ----------
echo -e "\n\033[32m✔ 安装完成验证:\033[0m"
google-chrome --version
"$INSTALL_DIR/chromedriver" --version
ffmpeg -version | head -n1 | awk '{print "FFmpeg版本:",$3}'